package main

import (
	"flag"
	"log"
	"os"
	"os/signal"
	"syscall"

	_ "github.com/GoAdminGroup/go-admin/adapter/gin"                 // web framework adapter
	_ "github.com/GoAdminGroup/go-admin/modules/db/drivers/postgres" // sql driver
	_ "github.com/GoAdminGroup/themes/adminlte"                      // ui theme
	_ "github.com/GoAdminGroup/themes/sword"                         // ui theme

	"github.com/GoAdminGroup/go-admin/engine"

	"mini-admin/internal/conf"
	"mini-admin/internal/http"
)

func main() {
	flag.Parse()
	err := conf.Init()
	eng := engine.Default().AddConfig(conf.Conf.Admin)
	if err != nil {
		panic(err.Error())
	}
	http.Init(eng)

	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
	for {
		s := <-c
		log.Printf("mini-admin get a signal: %d", s)
		switch s {
		case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
			eng.PostgresqlConnection().Close()
			return
		case syscall.SIGHUP:
		default:
			return
		}
	}
}
